package com.lucenten.collect.gnss.service.impl;

import javax.annotation.Resource;

import cn.quevo.common.entity.PageResult;

import com.lucenten.collect.config.entity.SequenceIndex;
import com.lucenten.collect.config.entity.query.SequenceIndexQuery;
import com.lucenten.collect.config.service.SequenceIndexService;
import com.lucenten.collect.gnss.handler.tools.ReNameTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lucenten.collect.gnss.dao.SatelliteStationDao;
import com.lucenten.collect.gnss.entity.SatelliteStation;
import com.lucenten.collect.gnss.entity.query.SatelliteStationQuery;
import com.lucenten.collect.gnss.service.SatelliteStationService;

import java.util.List;
import java.util.Map;

/**
 * 卫星台站信息 Service接口实现
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2019/04/26 22:44:38
 * @Copyright 东方浩星（北京）传媒科技有限公司版权所有.All Rights Reserved.
 */
@Service("satelliteStationService")
@Transactional(transactionManager = "transactionManager", rollbackFor = {Exception.class})
public class SatelliteStationServiceImpl implements SatelliteStationService {
    @Resource(name = "satelliteStationDao")
    private SatelliteStationDao satelliteStationDao;
    @Resource(name = "sequenceIndexService")
    private SequenceIndexService sequenceIndexService;
    @Override
    public Map<String, Object> getSinge(SatelliteStationQuery satelliteStationQuery) {
        return this.satelliteStationDao.getSinge(satelliteStationQuery);
    }

    @Override
    public Integer create(SatelliteStation satelliteStation) {
        satelliteStation.validateCreate();
        SatelliteStationQuery satelliteStationQuery = SatelliteStationQuery.builder().stationName(satelliteStation.getStationName())
                .latitude(satelliteStation.getLatitude()).longitude(satelliteStation.getLongitude()).itudeYn("Y").build();
        if (this.satelliteStationDao.checkExists(satelliteStationQuery) > 0) {
            satelliteStation.setReStationName(satelliteStation.getStationName());
            return 1;
        }
        satelliteStation.setReStationName(this.gerRename(satelliteStation));
        return this.satelliteStationDao.create(satelliteStation);
    }

    @Override
    public Integer update(SatelliteStation satelliteStation) {
        satelliteStation.validateUpdate();
        return this.satelliteStationDao.update(satelliteStation);
    }

    @Override
    public PageResult<Map<String, Object>> getPage(SatelliteStationQuery satelliteStationQuery) {
        return this.satelliteStationDao.getPage(satelliteStationQuery);
    }

    @Override
    public List<SatelliteStation> getSatelliteStationList(SatelliteStationQuery satelliteStationQuery) {
        return this.satelliteStationDao.getSatelliteStationList(satelliteStationQuery);
    }

    private String gerRename(SatelliteStation satelliteStation){
        SatelliteStationQuery satelliteStationQuery = SatelliteStationQuery.builder().stationName(satelliteStation.getStationName()).build();
        if (this.satelliteStationDao.checkExists(satelliteStationQuery) <= 0) {
            return satelliteStation.getStationName();
        }
        /**
         * 获取新名称后，需要再次验证是否已存在
         */
        while (true) {
            SequenceIndexQuery sequenceIndexQuery = SequenceIndexQuery.builder().seqCode("STATION_RENAME").build();
            SequenceIndex sequenceIndex = this.sequenceIndexService.getNext(sequenceIndexQuery);
            satelliteStationQuery.setStationName(ReNameTools.getReName(sequenceIndex.getSeqCount()));
            if (this.satelliteStationDao.checkExists(satelliteStationQuery) <= 0) {
                break;
            }
        }
        return satelliteStationQuery.getStationName();
    }

}
